package simple.utils.data.serializers;

import com.google.protobuf.Message;

import java.io.IOException;

/**
 * Created by rehak on 4/25/14.
 */
public class GPBSerializer {

    private GPBSerializer() {
    }

    public static <E extends Message> Serializer<E> get(final E prototype) {
        return new Serializer<E>() {
            @Override
            public byte[] toBytes(E e) throws IOException {
                return e.toByteArray();
            }

            @Override
            public E fromBytes(byte[] data) throws IOException {
                return (E) prototype.newBuilderForType().mergeFrom(data).build();
            }
        };
    }

}
